package com.ruibang.glass.portal.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.portal.entity.Product;
import com.ruibang.glass.portal.mapper.ProductMapper;
import com.teaming.cloud.framework2.common.constant.CommonConstant;
import com.teaming.cloud.framework2.common.pojo.PageRequest;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 电脑设置状态信息 服务实现类
 * </p>
 *
 * @author wujie
 * @since 2020-08-06
 */
@Service
public class ProductService extends ServiceImpl<ProductMapper, Product> implements IService<Product> {

    /**
     * 分页列表查询产品管理信息
     *
     * @param pageRequest
     * @return
     */
    public PageResult<Product> queryPage(PageRequest<Product> pageRequest) {
        final Product param = pageRequest.getParam();
        final LambdaQueryWrapper<Product> lambda = new QueryWrapper<Product>().lambda();
        this.buildCondition(lambda, param);
        final IPage<Product> page = this.page(new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize()), lambda);
        PageResult<Product> pr = new PageResult(page);
        return pr;
    }

    /***
     * 删除
     * @param id
     */
    public void delete(String id) {
        lambdaUpdate().eq(Product::getProductId, id).set(Product::getIsDelete, CommonConstant.ONE_STR).update();
    }

    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<Product> lambda, Product param) {
        lambda.eq(Product::getIsDelete, CommonConstant.ZERO_STR);
        if (null == param) return;
        if (StringUtils.isNotBlank(param.getProductCode())) {
            lambda.like(Product::getProductCode, param.getProductCode());
        }
        if (StringUtils.isNotBlank(param.getProductName())) {
            lambda.like(Product::getProductName, param.getProductName());
        }
        if (StringUtils.isNotBlank(param.getStandard())) {
            lambda.like(Product::getStandard, param.getStandard());
        }
        if (StringUtils.isNotBlank(param.getProductType())) {
            lambda.eq(Product::getProductType, param.getProductType());
        }
        if (CollectionUtils.isNotEmpty(param.getProductCodeList())) {
            lambda.in(Product::getProductCode, param.getProductCodeList());
        }
        if (CollectionUtils.isNotEmpty(param.getProductNameList())) {
            lambda.in(Product::getProductName, param.getProductNameList());
        }
        if (CollectionUtils.isNotEmpty(param.getStandardList())) {
            lambda.in(Product::getStandard, param.getStandardList());
        }

        lambda.orderBy(true, false, Product::getProductId);
    }


}
